package newsDemo.controller;

import newsDemo.pojo.Result;
import newsDemo.service.newsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/news")
public class newsController {

    @Autowired
    private newsService newsService;

    @GetMapping("/tag/{tag}")
    public Result tag(@PathVariable int tag) {
        if(tag <= 0 || tag >= 6)  return Result.fail(10001, "参数越界");
        return Result.success(newsService.findByTag(tag));
    }

    @GetMapping("/id/{id}")
    public Result id(@PathVariable int id) {
        if(id <= 0 || id >= 26)  return Result.fail(10001, "参数越界");
        return Result.success(newsService.findById(id));
    }

    @GetMapping("/getRandNews")
    public Result getRandNews() {
        List list = new ArrayList();
        for(int i = 0; i < 10; i++) {
            int n = (int)(Math.random() * 100) % 16 + 1;
            list.add(newsService.findById(n));
        }
        return Result.success(list);
    }
}
